#include "dialog_inputcode.h"
#include "messageboxutil.h"
#include "ui_dialog_inputcode.h"

#include <QMessageBox>
#include <QRegExpValidator>
#include <QDebug>

Dialog_InputCode::Dialog_InputCode(QWidget *parent, const QString &productCode, const QString &workGroupName, const QString &workGroupSize, const QString &productClass) :
    QDialog(parent),
    m_productCode(productCode),
    m_groupName(workGroupName),
    m_groupSize(workGroupSize),
    m_productClass(productClass),
    ui(new Ui::Dialog_InputCode)
{
    ui->setupUi(this);


    // 创建一个 QRegExp 对象，匹配任何数字或字母
    QRegExp regExp("[A-Za-z0-9]+"); // 这将匹配一个或多个字母或数字
    // 创建一个 QRegExpValidator 对象，使用上面的 QRegExp
    QRegExpValidator *validator = new QRegExpValidator(regExp, ui->lineEdit_Code);

    // 将验证器设置到 QLineEdit
    ui->lineEdit_Code->setValidator(validator);


    ui->lineEdit_Code->setText(m_productCode);
    ui->lineEdit_productClass_get->setText(m_productClass);


    m_iniManager = new iniManager("./config/workGroupConfig.ini");

    QString Group = m_iniManager->getGroup("productClass", productClass);
//    ui->lineEdit_productClass_curr->setText(m_iniManager->readValue("current", "group").toString());
    ui->lineEdit_productClass_curr->setText(m_iniManager->readValue(Group, "productClass").toString());


    ui->LineEdit_workGroupName->setText(workGroupName);
    ui->LineEdit_workGroupSize->setText(workGroupSize);

    QStringList arg1List = iniManager::splitString(workGroupName);
    qDebug() << arg1List;

    if(arg1List.isEmpty())
    {
        qDebug() << "工作组编号，拆分错误";
        if(QMessageBox::Yes == MessageBoxUtil::showMessage(this, MessageBoxUtil::Warning, "警告", QString("显示:\n工作组：%1 格式不正确!").arg(workGroupName)))
        {

        }
        return;
    }

    QString currGroupId = arg1List.at(0);

    ui->lineEdit_groupCurrentSize->setText(m_iniManager->getGroupType(arg1List.at(4)));

    // 连接按钮的信号到槽函数
    connect(ui->btn_enter, &QPushButton::clicked, this, [=]() {

        if(ui->lineEdit_groupCurrentSize->text() != ui->lineEdit_devCurrentSize->text())
        {

            return;
        } else {

        }

//        if (QMessageBox::Yes == QMessageBox::question(this, "确认尺寸", QString("请确认该产品的尺寸为: %1").arg(m_groupSize), QMessageBox::Yes | QMessageBox::No, QMessageBox::No)) {
//            emit inputCode(ui->lineEdit_Code->text());
//            accept();
//        }

        if(QMessageBox::Yes == MessageBoxUtil::showMessage(this, MessageBoxUtil::Warning, "确认尺寸",
                                                           QString("请确认该产品的尺寸为: %1\n产品类型为：%2").arg(m_groupSize).arg(ui->lineEdit_groupCurrentSize->text())))
        {
            emit inputCode(ui->lineEdit_Code->text());
            accept();
        }
    });


}

Dialog_InputCode::~Dialog_InputCode()
{
    delete ui;

    if(m_iniManager != nullptr)
    {
        delete m_iniManager;
    }
}

void Dialog_InputCode::do_setCurrentGroupSize(const QString &currentGroupSize)
{
    ui->lineEdit_devCurrentSize->setText(currentGroupSize);

    if(ui->lineEdit_groupCurrentSize->text() != currentGroupSize)
    {
        ui->label_hint->setStyleSheet("color:green");
        ui->label_hint->setText("类型不匹配，请检查类型！");

//        ui->label_hint->show();
    } else {
        ui->label_hint->setStyleSheet("color:blue");
        ui->label_hint->setText(QString("请确保PLC配方的一致：%1").arg(ui->lineEdit_groupCurrentSize->text()));
//        ui->label_hint->hide();
    }
}

void Dialog_InputCode::on_btn_enter_clicked()
{

}

void Dialog_InputCode::on_btn_close_clicked()
{
    reject();
}
